package com.sgb.zhaocai.viewmodel;

import android.text.TextUtils;

import androidx.lifecycle.ViewModel;

import com.sgb.zhaocai.base.BaseApi;
import com.sgb.zhaocai.entity.MemberCompanyEntitly;
import com.sgb.zhaocai.view.fragment.PickMemberCompanyFragment;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.base.model.entity.ListResponse;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;
import com.swgk.core.util.PickSelectEntity;
import com.swgk.core.view.areapopup.AddressEntity;
import com.swgk.core.view.selection.CommonPopEntity;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class PickMemberCompanyModel extends ViewModel {
    PickMemberCompanyFragment fragment;
    private boolean isLastPage;
    private int page =1;
    public void setActivity(PickMemberCompanyFragment pickMemberCompanyFragment) {
        this.fragment = pickMemberCompanyFragment;
    }

    /**
     * 会员单位-企业列表
     */
    public void getMixMemberListByPage(boolean isLoadMore, String name, String memberCategory, String provinceNo, String cityNo, String level, List<String> biddingRegion) {
        page = isLoadMore? page+1:1;
        Map map = new HashMap();
        map.put("limit","20");
        map.put("page",page);
        map.put("memberType","1");//会员主体:1企业 2个人
        map.put("relation","2");
        map.put("status","2");
        map.put("name",name);
        map.put("memberCategory",memberCategory);//会员类型
        if (TextUtils.isEmpty(provinceNo)||TextUtils.isEmpty(cityNo)){
            map.put("areaNos",biddingRegion);
        }
        map.put("provinceNo",provinceNo);//省
        map.put("cityNo",cityNo);//市
        map.put("starsCope",level);//会员等级
        DialogHelper.showProgressDialog(fragment.getActivity(), null, "数据加载中", 0, true, null).setCanceledOnTouchOutside(false);
      BaseApi.getApi().getMixMemberListByPage(map).enqueue(new ICallBack<BaseEntity<ListResponse<List<MemberCompanyEntitly>>>>() {
            @Override
            public void onSuccess(BaseEntity<ListResponse<List<MemberCompanyEntitly>>> data) {
                DialogHelper.dismissProgressDialog();
                if (data.getState().equals(BaseApi.RESPONSE_OK)){
                    isLastPage = data.getData().getCurrentPage() >= data.getData().getPageSize();
                    for (MemberCompanyEntitly memberCompanyEntitly : data.getData().getList()) {
                        memberCompanyEntitly.setCheck(false);
                    }
                    fragment.setMixMemberListByPage(data.getData().getList(),isLastPage);
                }else {
                    MToast.showToast(fragment.getActivity(),data.getMsg());
                }
            }
        });
    }

    /**
     * 会员等级
     */
    public void getMemberGrade() {
        DialogHelper.showProgressDialog(fragment.getActivity(), null, "数据加载中", 0, true, null).setCanceledOnTouchOutside(false);
        BaseApi.getApi().getMemberGrade().enqueue(new ICallBack<BaseEntity<List<CommonPopEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<CommonPopEntity>> data) {
                DialogHelper.dismissProgressDialog();
                if (data.getState().equals(BaseApi.RESPONSE_OK)){
                    fragment.setMemberGradeList(data.getData());
                }else {
                    MToast.showToast(fragment.getActivity(),data.getMsg());
                }
            }
        });
    }

    /**
     * 地址
     */
    public void getAddressModel() {
        DialogHelper.showProgressDialog(fragment.getActivity(), null, "数据加载中", 0, true, null).setCanceledOnTouchOutside(false);
        BaseApi.getApi().getAddressEntityCityList().enqueue(new ICallBack<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>>() {
            @Override
            public void onSuccess(BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>> data) {
                DialogHelper.dismissProgressDialog();
                if (data != null && data.getData() != null && data.getData().size() > 0) {
                    fragment.setAddressList(data.getData());
                }
            }
        });
    }

    /**
     * 会员类型
     */
    public void getMemberTypeList() {
       /* Map map = new HashMap();
        map.put("ascription","0");
        map.put("businessNo","");*/
        DialogHelper.showProgressDialog(fragment.getActivity(), null, "数据加载中", 0, true, null).setCanceledOnTouchOutside(false);
        BaseApi.getApi().getMemberTypeList("0").enqueue(new ICallBack<BaseEntity<List<PickSelectEntity<List<PickSelectEntity<List<PickSelectEntity<List<PickSelectEntity<List<PickSelectEntity>>>>>>>>>>>() {
            @Override
            public void onSuccess(BaseEntity<List<PickSelectEntity<List<PickSelectEntity<List<PickSelectEntity<List<PickSelectEntity<List<PickSelectEntity>>>>>>>>>> data) {
                DialogHelper.dismissProgressDialog();
                if (data.getState().equals(BaseApi.RESPONSE_OK)){
                    fragment.setMemberTypeList(data.getData());
                }else {
                    MToast.showToast(fragment.getActivity(),data.getMsg());
                }
            }
        });
    }

    /**
     * 获取对应订单地址
     * @param id
     * @param fromWhere
     */
    public void getBiddingRegionAddressModel(String id, String fromWhere) {
        DialogHelper.showProgressDialog(fragment.getActivity(), null, "数据加载中", 0, true, null).setCanceledOnTouchOutside(false);
        BaseApi.getApi().getbiddingRegionAddressList(id,fromWhere).enqueue(new ICallBack<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>>() {
            @Override
            public void onSuccess(BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>> data) {
                DialogHelper.dismissProgressDialog();
                if (data != null && data.getData() != null && data.getData().size() > 0) {
                    fragment.setAddressList(data.getData());
                }
            }
        });
    }
}
